﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Reflection;
using System.Web.Mvc;
using easyccms.BusinessLayer.Repositories;
using easyccms.WebUI.Areas.Administrator.Models;
using easyccms.WebUI.Code;

namespace easyccms.WebUI.Areas.Administrator.Controllers
{
    [Authorize(Roles = "Administrator")]
    public class ManagementController : Controller
    {
        
        private UserProvider _userProvider
        {
            get{return new UserProvider();}
        }

        

        //
        // GET: /Administrator/Management/

        public ActionResult Index()
        {

            string sessionId = (Session["sessionId"] == null) ? Session.SessionID : Session["sessionId"].ToString();
            Session["sessionId"] = Session.SessionID;
            ViewData["sessionId"] = sessionId;
            return View();
        }

        public ActionResult Bin()
        {
            string data = string.Empty;
            BinModel model = new BinModel { BinCollection = new List<string>() };
            foreach (string controllerName in GetControllerNames())
            {
                string temp = controllerName.Replace("Controller", "");

                data += controllerName;

                model.BinCollection.Add(temp);
            }

            ViewData["controllers"] = data;
            ViewData["SelectedTab"] = TempData["SelectedTab"];
            return View(model);
        }

        public ActionResult Settings()
        {
            
            return View();
        }

        /// <summary>
        /// Userses the online.
        /// </summary>
        /// <returns>Partial view with collection of users online</returns>
        public ActionResult UsersOnline()
        {
            ////delete every user online who has not been logged within 15 min.
            //DateTime userExpiry = DateTime.Now.AddMinutes(15);

            //foreach (UserOnline userOnline in this._userOnlineRepository.All.Where(x=>x.LastAction > userExpiry))
            //{
            //    this._userOnlineRepository.Delete(userOnline);    
            //}

            //return View(this._userOnlineRepository.All.AsEnumerable());
            return View();
        }

        /// <summary>
        /// Contacts the us.
        /// </summary>
        /// <returns>Action view with contact details.</returns>
        public ActionResult ContactUs()
        {
            return View();
        }

        public ActionResult UserLoggedIn()
        {
            return View(this._userProvider.GetCurrentUser());
        }


        /// <summary>
        /// Gets the sub classes.
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <returns></returns>
        [NonAction]
        private static List<Type> GetSubClasses<T>()
        {
            return Assembly.GetCallingAssembly().GetTypes().Where(x => x.Namespace == "easyccms.WebUI.Areas.Administrator.Controllers").Where(
                type => type.IsSubclassOf(typeof(T))
                ).ToList();
        }
        [NonAction]
        public IEnumerable<string> GetControllerNames()
        {
            List<string> controllerNames = new List<string>();

            GetSubClasses<Controller>().ForEach(
                type => controllerNames.Add(type.Name)
                );
            var list = controllerNames.OrderBy(x => x);
                //from s in controllerNames
                //       orderby s
                //       select s;
            return list;
        }
    }
}
